package com.app.newiptv.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.app.newiptv.R;
import com.app.newiptv.adaper.ChanelAllAdapter;
import com.app.newiptv.adaper.MainChanelAdapter;
import com.app.newiptv.adaper.ProgramAdapter;
import com.app.newiptv.base.MyApplication;
import com.app.newiptv.presenter.AllChannelsPresenter;
import com.app.newiptv.utils.Paths;
import com.app.newiptv.view.FocusGridLayoutManager;
import com.app.newiptv.view.MyRecycleView;

public class AllChannelsActivity extends Activity implements AllChannelsPresenter.Callback {
    private MyRecycleView mOne_stage_rv;
    private MyRecycleView mTwo_stage_rv;
    private MyRecycleView mThree_stage_rv;
    private View mline_two;
    private View mline_one;
    private AllChannelsPresenter allChannelsPresenter;
    private int focusPosition;
    private MainChanelAdapter firstAdapter;
    private ChanelAllAdapter sencondAllAdapter;
    private ProgramAdapter thirdAdapter;
    private TextView test_show_one;
    private TextView test_show_two;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_channels);
        initView();
        initData();
        setLisenter();
    }

    private void initView() {
        mOne_stage_rv = (MyRecycleView) findViewById(R.id.one_stage_rv);
        mTwo_stage_rv = (MyRecycleView) findViewById(R.id.two_stage_rv);
        mThree_stage_rv = (MyRecycleView) findViewById(R.id.three_stage_rv);
        mline_two = findViewById(R.id.line_two);
        mline_one = findViewById(R.id.line_one);
        test_show_one = (TextView) findViewById(R.id.test_show_one);
        test_show_two = (TextView) findViewById(R.id.test_show_two);
        test_show_one.setTypeface(MyApplication.face);
        test_show_two.setTypeface(MyApplication.face);
    }

    private void initData() {
        allChannelsPresenter = new AllChannelsPresenter(this);
        allChannelsPresenter.setDisplayed(true);
        Bundle bundle = new Bundle();
        bundle.putString("url", Paths.HOSTPATH + Paths.CHANNEL_PATH);
        allChannelsPresenter.loadItem(bundle, this);
        mOne_stage_rv.setFocusable(false);
        mTwo_stage_rv.setFocusable(false);
        mThree_stage_rv.setFocusable(false);
        test_show_one.setText("选中呼出频道列表");
        test_show_two.setText("后退关闭频道列表");

    }

    private void setLisenter() {
        allChannelsPresenter.onBindAllChannels(channelsViewHolder);
        mOne_stage_rv.setOnKeyListener(new FirstChannelOnKey());
        mTwo_stage_rv.setOnKeyListener(new SencondChannelOnKey());
        mThree_stage_rv.setOnKeyListener(new ThirdChannelOnKey());
        mOne_stage_rv.requestFocus();
        mOne_stage_rv.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    //Toast.makeText(AllChannelsActivity.this, "获取到焦点", Toast.LENGTH_SHORT).show();
                    mOne_stage_rv.post(new Runnable() {
                        @Override
                        public void run() {
                            if (mOne_stage_rv.getChildCount() > 0) {
                                firstAdapter.selected();
                            }
                        }
                    });
                }
            }
        });
        mTwo_stage_rv.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    mTwo_stage_rv.post(new Runnable() {
                        @Override
                        public void run() {
                            if (mTwo_stage_rv.getChildCount() > 0) {
                                sencondAllAdapter.selected();
                            }
                        }
                    });
                }
            }
        });
        mThree_stage_rv.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    mThree_stage_rv.post(new Runnable() {
                        @Override
                        public void run() {
                            //Toast.makeText(AllChannelsActivity.this, "获取到焦点", Toast.LENGTH_SHORT).show();
                            if (mThree_stage_rv.getChildCount() > 0) {
                                thirdAdapter.selected();
                            }
                        }
                    });
                }
            }
        });
    }


    @Override
    public void onError(int code, String msg) {

    }

    @Override
    public void onMovieCompleted(AllChannelsPresenter presenter, int count) {
        firstAdapter = new MainChanelAdapter(presenter);
        mOne_stage_rv.setLayoutManager(new FocusGridLayoutManager(AllChannelsActivity.this, 1));
        firstAdapter.setCount(count);
        mOne_stage_rv.setAdapter(firstAdapter);
        mOne_stage_rv.setFocusable(true);
    }

    @Override
    public void onChannelCompleted(AllChannelsPresenter presenter, int count) {
        sencondAllAdapter = new ChanelAllAdapter(presenter);
        FocusGridLayoutManager manager = new FocusGridLayoutManager(AllChannelsActivity.this, 1);
        mTwo_stage_rv.setLayoutManager(manager);
        sencondAllAdapter.setCount(count);
        mTwo_stage_rv.setAdapter(sencondAllAdapter);
        mTwo_stage_rv.setFocusable(true);
    }

    @Override
    public void onProgramCompleted(AllChannelsPresenter presenter, int count) {
        thirdAdapter = null;
        thirdAdapter = new ProgramAdapter(presenter, AllChannelsActivity.this);
        mThree_stage_rv.setLayoutManager(new FocusGridLayoutManager(AllChannelsActivity.this, 1));
        thirdAdapter.setCount(count);
        mThree_stage_rv.setAdapter(thirdAdapter);
        mThree_stage_rv.setFocusable(true);
    }

    private AllChannelsPresenter.AllChannelsViewHolder channelsViewHolder = new AllChannelsPresenter.AllChannelsViewHolder() {
        @Override
        public void setmThreeStageRv(boolean isShow) {
            if (isShow) {
                mThree_stage_rv.setVisibility(View.VISIBLE);
            } else {
                mThree_stage_rv.setVisibility(View.GONE);
            }
        }

        @Override
        public void setLineTwoView(boolean isShow) {
            if (isShow) {
                mline_two.setVisibility(View.VISIBLE);
            } else {
                mline_two.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void setLineOneView(boolean isShow) {
            if (isShow) {
                mline_one.setVisibility(View.VISIBLE);
            } else {
                mline_one.setVisibility(View.INVISIBLE);
            }
        }
    };

    public class FirstChannelOnKey implements View.OnKeyListener {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            int action = event.getAction();
            if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                if (action == KeyEvent.ACTION_DOWN) {
                    focusPosition = firstAdapter.selectedNext(); // 下面是否有item
                    //Toast.makeText(AllChannelsActivity.this, "下==》："+ focusPosition, Toast.LENGTH_SHORT).show();
                }
                return true;
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                if (action == KeyEvent.ACTION_DOWN) {
                    focusPosition = firstAdapter.selectedPre();
                    //Toast.makeText(AllChannelsActivity.this, "上==》："+ focusPosition, Toast.LENGTH_SHORT).show();
                }
                return true;
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && event.getAction() == KeyEvent.ACTION_DOWN) {
                mTwo_stage_rv.requestFocus();
                return true;
            }
            return false;
        }
    }

    public class SencondChannelOnKey implements View.OnKeyListener {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            int action = event.getAction();
            if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                if (action == KeyEvent.ACTION_DOWN) {
                    focusPosition = sencondAllAdapter.selectedNext(); // 下面是否有item
                    //Toast.makeText(AllChannelsActivity.this, "下==》："+ focusPosition, Toast.LENGTH_SHORT).show();
                }
                return true;
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                if (action == KeyEvent.ACTION_DOWN) {
                    focusPosition = sencondAllAdapter.selectedPre();
                    //Toast.makeText(AllChannelsActivity.this, "上==》："+ focusPosition, Toast.LENGTH_SHORT).show();
                }
                return true;
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && event.getAction() == KeyEvent.ACTION_DOWN) {
                mThree_stage_rv.requestFocus();
                return true;
            } else if ((keyCode == KeyEvent.KEYCODE_ENTER||keyCode == KeyEvent.KEYCODE_DPAD_CENTER) && event.getAction() == KeyEvent.ACTION_DOWN) {
                sencondAllAdapter.onClick();
            }
            return false;
        }
    }

    public class ThirdChannelOnKey implements View.OnKeyListener {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            int action = event.getAction();
            if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                if (action == KeyEvent.ACTION_DOWN) {
                    focusPosition = thirdAdapter.selectedNext(); // 下面是否有item
                    //Toast.makeText(AllChannelsActivity.this, "下==》："+ focusPosition, Toast.LENGTH_SHORT).show();
                }
                return true;
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                if (action == KeyEvent.ACTION_DOWN) {
                    focusPosition = thirdAdapter.selectedPre();
                    //Toast.makeText(AllChannelsActivity.this, "上==》："+ focusPosition, Toast.LENGTH_SHORT).show();
                }
                return true;
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                if (action == KeyEvent.ACTION_DOWN) {
                    mTwo_stage_rv.requestFocus();
                    //Toast.makeText(AllChannelsActivity.this, "上==》："+ focusPosition, Toast.LENGTH_SHORT).show();
                }
                return true;
            }else if((keyCode == KeyEvent.KEYCODE_DPAD_CENTER||keyCode == KeyEvent.KEYCODE_ENTER)&&action == KeyEvent.ACTION_DOWN){
                thirdAdapter.onClick();
                return true;
            }
            return false;
        }
    }
}
